/**
 *
 * @Author: Marek Zakowicz <mazak (at) debian (dot) linux.org.pl>
 *
 * Copyright (c) 2005, 2006, 2007 Marek Zakowicz. All rights reserved.
 *
 * Licence: This code may be used, (re)distribute and/or modify under the terms one of the following two kinds of licence:
 *          1) as free software under GPL 3 (General Public Licence ver. as published by the Free Software Foundation)
 *          2) as software under the terms individual licence from Marek Zakowicz
 *
 *          This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 *          without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 */

package jsp2php;

import java.util.regex.*;
import java.io.*;

import antlr.*;

import org.apache.commons.lang.StringEscapeUtils;

public class Java2PHPConverter {

   private OutputStream           out;
   private JavaFragmentTranslator jbt = new JavaFragmentTranslator();
   private JSPFTranslator         tr  = new JSPFTranslator();

   private void print(String s) {
      jbt.print(s);
   }

   void init(OutputStream out) {
      this.out = out;
      jbt.out  = out;
   }

   void processText(String text) {
      print( text );
   }

   void processCode(String code) throws Exception {
      print("<?");
      JavaFragmentLexer  lexer=new JavaFragmentLexer( new StringReader(code) );
      lexer.jbt  = jbt;
      JavaFragmentParser parser=new JavaFragmentParser(lexer);
      parser.jbt = jbt;
      parser.tr  = tr;
      try {
         parser.fragment();
      }
      catch(RecognitionException re) {
         throw new Exception(re.getMessage());
      }
      jbt.printWS=true;
      jbt.printWS("");
      print("?>");
      if( jbt.controlStarted ) {
         System.err.println("WARNING: Opened control statement on the end of scriptlet!  Probably bad translated!");
      }
   }

   public void processExpr(String exprString) throws Exception {
      print("<?= ");
      JavaFragmentLexer  lexer=new JavaFragmentLexer(new StringReader( exprString.trim() ));
      lexer.jbt=jbt;
      JavaFragmentParser parser=new JavaFragmentParser(lexer);
      parser.jbt=jbt;
      parser.tr  = tr;

      try {
         Translation expr = parser.expression2();
         jbt.printWS=true;

         if(expr.type == JavaType.bool()) {
            // TODO: ladniej byloby wywolac toString, czy valueOf...
            String text=expr.text;
            if(!expr.consistent && text.matches(".*(([^!<>=]=[^=])|(<<=)|(>>=)).*") ) {
               jbt.print( "( "+ expr.text +" ) ? \"true\" : \"false\"" );
            }
            else {
               jbt.print( expr.text +" ? \"true\" : \"false\"" );
            }
         }
         else {
            jbt.print( expr.text );
         }
      }
      catch(RecognitionException re) {
         throw new Exception(re.getMessage()); // przepakowanie wydaje mi sie konieczne, aby JavaBlockParser nie zaczal sterowac JSPParserem...
      }
      jbt.printWS=true;
      jbt.printWS("");
      print(" ?>");
   }

   void generateExternals() throws IOException {
      out.write( "<? require_once(\"jsp2php_addons.php\"); ?>".getBytes() );
   }

   void generateAddons() {
      print( AddOns.generate() );
   }
}

